﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Web.UI.HtmlControls;

namespace ORS.Metadata
{
    public partial class Metadata : System.Web.UI.Page
    {
        protected string xmldata;

        protected void Page_Load(object sender, EventArgs e)
        {
            xmllist();//加载XML解析文件列表
        }



        /// <summary>
        /// 上传和解析文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            string datafilename = uploadWenjian();  //上传文件的文件名

            string xmlfilename = this.HiddenField1.Value;//获取存放在隐藏控件的XML文件名

            JXwenjian(datafilename, xmlfilename);//解析文件

            //保存解析的信息到数据库
        }


        #region  加载XML文件列表
        private void xmllist()
        {
            string[] xmlfile = Directory.GetFiles(Server.MapPath("~/XML"), "*.xml");

            StringBuilder sb = new StringBuilder();
            
            for (int i = 0; i < xmlfile.Length; i++)
            {
                string xmlfilename = Path.GetFileName(xmlfile[i]);//获取文件名

                sb.AppendFormat("<option value=\"{0}\">{1}</option>", xmlfilename, xmlfilename);
            }

            xmldata = sb.ToString();
        }
        #endregion

        #region  上传数据文件，得到数据文件路径
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        private string uploadWenjian()
        {
            try
            {
                if (FileUpload1.PostedFile.FileName == "")
                {
                    Label1.Visible = true;
                    Label1.ForeColor = System.Drawing.Color.FromName("#ff0000");
                    Label1.Text = "请选择数据文件！";
                    return "";
                }
                else
                {
                    //if (FileUpload1.PostedFile.ContentLength > 500000)
                    //{
                    //    Label1.Visible = true;
                    //    Label1.ForeColor = System.Drawing.Color.FromName("#ff0000");
                    //    Label1.Text = "文件过大！";
                    //    return "";
                    //}
                    string datafilepath = FileUpload1.PostedFile.FileName;
                    string filename = datafilepath.Substring(datafilepath.LastIndexOf("."));
                    string dataname = DateTime.Now.ToFileTime() + filename;
                    string serverpath = Server.MapPath("~/dataPic/");
                    if (!Directory.Exists(serverpath))
                    {
                        Directory.CreateDirectory(serverpath);//创建该文件夹
                    }
                    FileUpload1.PostedFile.SaveAs(serverpath + dataname);
                    Label1.Visible = true;
                    Label1.ForeColor = System.Drawing.Color.FromName("#000000");
                    Label1.Text = "上传成功！";

                    return dataname;
                }
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
                return "";
            }
        }
        #endregion

        #region 解析数据文件，并放到相应的控件中
        private void JXwenjian(string datafilename, string xmfilename)
        {

        }
        #endregion

        #region  保存解析的信息到数据库
        protected void Button2_Click(object sender, EventArgs e)
        {

        }
        #endregion





    }
}